package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.QuizStartActivity
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import java.util.*

/**
 * Quizzes 两个list公用
 */
class QuizzesListAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataList = ArrayList<QuizBean>()
    private var loadFinished: Boolean = false
    private var mTitle = ""

    fun setData(list: ArrayList<QuizBean>) {
        mDataList.addAll(0, list)
        notifyItemRangeChanged(0, list.size)

    }

    fun setTitleOfFragment(title: String) {
        mTitle = title
    }

    fun setMoreData(list: ArrayList<QuizBean>) {
        mDataList.addAll(list)
        notifyItemRangeChanged(itemCount, list.size)
    }

    override fun getItemCount(): Int {
        return if (mDataList.isEmpty()) {
            0
        } else {
            mDataList.size + 1
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == mDataList.size) BOTTOM else CONTENT
    }

    companion object {
        const val BOTTOM = 0x13
        const val CONTENT = 0x14
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is FooterViewHolder -> {
                handleFooterViewHolder(holder)
            }
            is QuizzesViewHolder -> {
                val bean = mDataList[position]
                ImageLoader.loadImage(holder.bg, bean.image)
                holder.tvTitle.text = bean.title
                holder.tvPlays.text = StringBuilder(bean.players + "k plays")
                holder.itemView.setOnClickListener(View.OnClickListener {
                    if (SysUtils.isFastClick()) return@OnClickListener

                    FlurryEventUtil.putDataToEvent(EventType.TYPE_QUIZ_CLICK, null, null)
                    mContext.startActivity(QuizStartActivity.generateIntent(mContext, bean))
                })
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            BOTTOM -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.list_view_footer_comment, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.item_quizzes_newest,
                    parent, false
                )
                QuizzesViewHolder(view)
            }

        }
    }

    fun setLoadFinished(loadFinished: Boolean) {
        this.loadFinished = loadFinished
        notifyDataSetChanged()
        notifyItemChanged(itemCount)
    }

    /**
     * 所对应的具体布局
     * **/
    private fun handleFooterViewHolder(holder: QuizzesListAdapter.FooterViewHolder) {
        if (loadFinished) {
            if (holder.itemView.visibility != View.VISIBLE) {
                holder.itemView.visibility = View.VISIBLE
            }
            if (holder.progressBar.visibility != View.INVISIBLE) {
                holder.progressBar.visibility = View.INVISIBLE
            }
            holder.textView.visibility = View.VISIBLE
            holder.textView.text = mContext.getString(R.string.label_bottom)
            holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white_3))
        } else {
            if (holder.progressBar.visibility != View.VISIBLE) {
                holder.progressBar.visibility = View.VISIBLE
            }
            holder.textView.visibility = View.GONE
            //holder.textView.text = mContext.getString(R.string.dialog_progress_loading)
            //holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white))
        }
    }


    class QuizzesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val bg: ImageView = itemView.findViewById(R.id.image_view_quiz_bg)
        val tvPlays: TextView = itemView.findViewById(R.id.tv_plays)
    }

    class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val progressBar: ProgressBar = itemView.findViewById(R.id.list_view_foot_progress)
        val textView: TextView = itemView.findViewById(R.id.list_view_foot_more)
    }
}
